#pragma once

enum c6821_regs
	{
	c6821_ddra = 0,
	c6821_ddrb = 2,
	c6821_ctrla = 1,
	c6821_ctrlb = 3,
	};

//control register bits
#define CTRL_IRQ1_ENABLED		0x01
#define CTRL_IRQ2_ENABLED		0x08
#define CTRL_OUTPUT				0x04		//output enabled
#define CTRL_STROBE_E_RESET	0x08
#define CTRL_C2_SETMODE			0x10
#define CTRL_C2_OUTPUT			0x20

class C6821			//motorola 6821 pia
	{
	public:
		u8 DataA,DataB;				//data registers
		u8 ControlA,ControlB;		//control registers
		u8 InputA,OutputA;
		u8 InputB,OutputB;
		u8 InputCA1,InputCA2,OutputCA2;
		u8 InputCB1,InputCB2,OutputCB2;
		u8 IRQA1,IRQA2;
		u8 IRQB1,IRQB2;
		u8 IRQAState,IRQBState;
		u8 (*InputAFunc)(void*);
		u8 (*InputBFunc)(void*);
		u8 (*InputCA1Func)(void*);
		u8 (*InputCA2Func)(void*);
		u8 (*InputCB1Func)(void*);
		u8 (*InputCB2Func)(void*);
		void (*OutputAFunc)(void*,u8);
		void (*OutputBFunc)(void*,u8);
		void (*OutputCA2Func)(void*,u8);
		void (*OutputCB2Func)(void*,u8);
		void (*IRQAFunc)(void*);
		void (*IRQBFunc)(void*);
		void *user;
	public:
		C6821();
		~C6821();
		void SetUser(void *u){user = u;}
		void Init();
		void Reset();
		u8 Read(int reg);
		void Write(int reg,u8 data);
		void SetInputA(u8 data);
		void SetInputB(u8 data);

		void update_shared_irq_handler(void (*irq_func)(void*));
		void update_6821_interrupts();
		void set_input_ca1(int data);
		void set_input_ca2(int data);
		void set_input_cb1(int data);
		void set_input_cb2(int data);
	};
